var jCategoryTree;
$j(document).ready(function () {
	
});


//点击树节点动作
function clickTreeItemAction(){
	var treeItem=getCategoryItemJSON(jCategoryTree.getSelected());
	searchContents(true,{'categoryPath':treeItem.categoryPath});
}


//创建文章
function createContent(){
	var url = __ctxPath+"/content/content/ajaxPage.html?doAction=add&direct=false";
	if(jCategoryTree.getSelected().length != 0){
		var treeItem = getCategoryItemJSON(jCategoryTree.getSelected());
		url+= "&contentCategoryId="+treeItem.id;
	}
	showContentForm(url);
}

//编辑文章
function editContent(contentId){
	var url = __ctxPath+"/content/content/ajaxPage.html?doAction=edit&direct=false&contentId="+contentId;
	showContentForm(url);		
}

//在本页以ajaxPage方式打开contentForm 
function showContentForm(url,param){
	//在AJAX调用前初始化编辑器;并在回调函数使用KE.create()方法创建编辑器
	initHtmlEditor('contentBody',940,400);	
	$j('#mainDiv').hide();
	$j('#formDiv').show().loading().load(url,param,loadHtmlEditor);
}
/*******************************************
1.ajax请求的页面中，在发起AJAX请求前，初始化编辑器[KE.init({})]，否则无法使用编辑器
2.ajax返回HTML后，在回调函数里执行KE.create()创建编辑器
3.详见/StoreAdmin/web/scripts/kindeditor/kindeditor-init.js
*******************************************/
function loadHtmlEditor(){
	createHtmlEditor('contentBody');	
}

//删除多篇文章
function deleteContents(){
	var paraData=$j("#contentListForm :input[name='multiIds']").serializeArray();
	if(paraData.length>0){
		var itemNames = fnGetSelectedItemNames();
		if(confirm( __FMT.common_message_confirmDeleteThis+ " " + itemNames+"?")){
			$j.post(__ctxPath + "/content/content/blank.html?doAction=multiDelete", paraData, deleteContentsCallback);
		}
	}else{
		alert(__FMT.contentList_selectContent);
	}
}
//删除多篇文章后的回调函数
function deleteContentsCallback(){
	sysMsg(__FMT.contentList_multiDelete_successed);
	refreshContentList();
}

//多篇文章更新有效期
function multiUpdateExpiredContent(){
	var paraData=$j("#contentListForm :input[name='multiIds']").serializeArray();
	if(paraData.length>0){
		var itemNames = fnGetSelectedItemNames();
		if(confirm( __FMT.common_message_confirmUpdateExpiredThis+ " " + itemNames+"?")){
			$j.post(__ctxPath + "/content/content/blank.html?doAction=multiUpdateExpired", paraData, multiUpdateExpiredContentCallback);
		}
	}else{
		alert(__FMT.contentList_selectContent);
	}
}
//多篇文章更新有效期后的回调函数
function multiUpdateExpiredContentCallback(){
	sysMsg(__FMT.contentList_multiUpdateExpired_successed);
	refreshContentList();
}

//验证函数
function validateSearchCriteria(){
	return true;
}

//重设搜索条件
function resetSearchCriteria(){
	$("sc_keywords").value = "";
	$("sc_excludedKeywords").value = "";
	$("sc_sourceName").value = "";
	$("sc_tags").value = "";
	$("sc_categoryPaths").selectedIndex = -1;
	$("sc_status").selectedIndex = 0;
}

//对已有结果进行排序,ascValue表示升序时对应的值,descValue表示降序时对应的值
function sortField(linkDOM,ascValue,descValue){
	var jThDOM = $j(linkDOM).parent('th');
	if(jThDOM.hasClass("order1")){
		$j('#storageOrderby').val(descValue);
	}else{
		$j('#storageOrderby').val(ascValue);
	}
	searchContents(false);
}


//搜索文章 isNew标示是否新的搜索,param标示是否自定义参数搜索
function searchContents(isNew,param){
	var postData;
	var url;
	if(isNew){
		if(null==param){
			if(validateSearchCriteria()){
				//有搜索结果搜索
			    postData = $j('#contentSearchCriteria :input').serializeArray();
			    url=__ctxPath+"/content/content/blank.html?doAction=search&btnSearch=1";
				$j("#contentResults").loading().load(url,postData);  
		    }else{
		    	return;
		    }
		}else{
			//新的搜索。全部，上架等
			//有搜索结果搜索
			if(param.status  != undefined){
				$j('#storageStatus').val(""+param.status);
			}
			if(param.categoryId  != undefined){
				$j('#storageCategoryId').val(""+param.categoryId);
			}
			if(param.categoryPath  != undefined){
				$j('#storageCategoryPath').val(""+param.categoryPath);
			}
		    postData = $j('#contentResults :input').serializeArray();
		  	url=__ctxPath+"/content/content/blank.html?doAction=search&btnSearch=1";
			$j("#contentResults").loading().load(url,postData); 
		}    
	}else{
		postData = $j('#contentResults :input').serializeArray();
		url=__ctxPath+"/content/content/blank.html?doAction=search";
		$j("#contentResults").loading().load(url,postData);  
	} 
	
}
//刷新文章列表
function refreshContentList(){
	searchContents(false);
}
//分页
function fnOnGoToPage(){
	refreshContentList();
}
